﻿using System;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Description;

namespace Tiscs.Practices.Web.Services
{
    /// <summary>
    /// 身份验证服务工厂。
    /// </summary>
    public class AuthenticationServiceHostFactory : ServiceHostFactory
    {
        /// <summary>
        /// 创建服务宿主。
        /// </summary>
        /// <param name="serviceType">服务类型。</param>
        /// <param name="baseAddresses">基地址。</param>
        /// <returns>服务宿主实例。</returns>
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            AuthenticationServiceHost host = new AuthenticationServiceHost(serviceType, baseAddresses);
            ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (smb == null)
            {
                host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true, HttpsGetEnabled = true });
            }
            else
            {
                smb.HttpGetEnabled = true;
                smb.HttpsGetEnabled = true;
            }
            ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
            if (sdb == null)
            {
                host.Description.Behaviors.Add(new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
            }
            else
            {
                sdb.IncludeExceptionDetailInFaults = true;
            }
            return host;
        }
    }
}
